import 'package:json_annotation/json_annotation.dart';



part 'collect_list_item.g.dart';

@JsonSerializable(
  explicitToJson: true,
  includeIfNull: false,
)
class CollectListItem {
  @JsonKey(name: 'post_id')
  final String postId;

  @JsonKey(name: 'title', defaultValue: '')
  final String title;

  @JsonKey(name: 'content', defaultValue: '')
  final String content;

  CollectListItem({
    required this.postId,
    required this.title,
    this.content = '',
  });

  factory CollectListItem.fromJson(Map<String, dynamic> json) {
    return CollectListItem(
      postId: json['post_id'].toString(), // 直接调用 toString() 转换
      title: json['title'] as String? ?? '',
      content: json['content'] as String? ?? '',
    );
  }

  Map<String, dynamic> toJson() => _$CollectListItemToJson(this);

  // 其他方法保持不变
  String get postIdValue => postId;

  static CollectListItem empty() => CollectListItem(
    postId: '0',
    title: '',
  );

  bool get isValid => postId != '0' && title.isNotEmpty;

  @override
  String toString() => 'CollectListItem(postId:$postId, title:$title)';

  CollectListItem copyWith({
    String? postId,
    String? title,
    String? content,
  }) {
    return CollectListItem(
      postId: postId ?? this.postId,
      title: title ?? this.title,
      content: content ?? this.content,
    );
  }
}

